package com.all.ui;

import java.util.Random;

import com.all.base.BaseActivity;
import com.all.common.drag.Callback;
import com.all.common.drag.Invoker;
import com.all.ui.adapter.ImageGriviewAdapter;
import com.all.utils.LocalImageUtils;
import com.all.widget.layout.DragLayout;
import com.all.widget.layout.DragLayout.DragListener;
import com.nineoldandroids.view.ViewHelper;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DragActivity extends BaseActivity {

	private DragLayout dl;
	private GridView gv_img;
	private ImageGriviewAdapter adapter;
	private ListView lv;
	private TextView tv_noimg;
	private ImageView iv_icon, iv_bottom;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drag);
		init();
		initDragLayout();
		LocalImageUtils.initImageLoader(mContext);
	}
	
	private void init() {
		// TODO Auto-generated method stub
		iv_icon = (ImageView) findViewById(R.id.iv_icon);
		iv_bottom = (ImageView) findViewById(R.id.iv_bottom);
		gv_img = (GridView) findViewById(R.id.gv_img);
		tv_noimg = (TextView) findViewById(R.id.iv_noimg);
		gv_img.setFastScrollEnabled(true);
		adapter = new ImageGriviewAdapter(this);
		gv_img.setAdapter(adapter);
		
		gv_img.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(DragActivity.this,
						ImageActivity.class);
				intent.putExtra("path", adapter.getItem(position));
				startActivity(intent);
			}
		});
		lv = (ListView) findViewById(R.id.lv);
		lv.setAdapter(new ArrayAdapter<String>(DragActivity.this,
				R.layout.list_drag_item, new String[] { "NewBee", "ViCi Gaming",
						"Evil Geniuses", "Team DK", "Invictus Gaming", "LGD",
						"Natus Vincere", "Team Empire", "Alliance", "Cloud9",
						"Titan", "Mousesports", "Fnatic", "Team Liquid",
						"MVP Phoenix", "NewBee", "ViCi Gaming",
						"Evil Geniuses", "Team DK", "Invictus Gaming", "LGD",
						"Natus Vincere", "Team Empire", "Alliance", "Cloud9",
						"Titan", "Mousesports", "Fnatic", "Team Liquid",
						"MVP Phoenix" }));
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				//Util.t(getApplicationContext(), "click " + position);
			}
		});
		iv_icon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				//dl.open();
			}
		});
	}
	
	private void initDragLayout() {
		dl = (DragLayout) findViewById(R.id.dl);
		dl.setDragListener(new DragListener() {
			@Override
			public void onOpen() {
				lv.smoothScrollToPosition(new Random().nextInt(30));
			}

			@Override
			public void onClose() {
				//shake();
			}

			@Override
			public void onDrag(float percent) {
				ViewHelper.setAlpha(iv_icon, 1 - percent);
			}
		});
	}
	@Override
	protected void onResume() {
		super.onResume();
		loadImage();
	}
	
	private void loadImage() {
		// TODO Auto-generated method stub
		new Invoker(new Callback() {

			@Override
			public void onBefore() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public boolean onRun() {
				// TODO Auto-generated method stub
				adapter.addAll(LocalImageUtils.getGalleryPhotos(DragActivity.this));
				return adapter.isEmpty();
			}

			@Override
			public void onAfter(boolean b) {
				// TODO Auto-generated method stub
				adapter.notifyDataSetChanged();
			}
			
		}).start();
				
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}

}
